<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="180px">
    <el-form-item label="批次号" prop="batchNumber">
      <el-input v-model="dataForm.batchNumber" placeholder="批次号" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="用户手机号码" prop="mobilePhone">
      <el-input v-model="dataForm.mobilePhone" placeholder="用户手机号码" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="序列号" prop="serialNumber">
      <el-input v-model="dataForm.serialNumber" placeholder="序列号" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="发送日期" prop="sendDate">
      <el-date-picker
        v-model="dataForm.birthDay"
        type="date"
        placeholder="选择发送日期"
        format="yyyy-MM-dd"
        value-format="timestamp">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="请求时间" prop="requestTime">
      <el-date-picker
        v-model="dataForm.birthDay"
        type="datetime"
        placeholder="选择请求日期"
        format="yyyy-MM-dd HH:mm:ss"
        value-format="timestamp">
      </el-date-picker>
    </el-form-item>
    <!--
    <el-form-item label="请求次数" prop="requestCount">
      <el-input v-model="dataForm.requestCount" placeholder="请求次数"></el-input>
    </el-form-item>
    <el-form-item label="请求状态" prop="requestStatus">
      <el-input v-model="dataForm.requestStatus" placeholder="请求状态"></el-input>
    </el-form-item>
    -->
    <el-form-item label="短信方返回的消息id" prop="messageId">
      <el-input v-model="dataForm.messageId" placeholder="短信方返回的消息id"></el-input>
    </el-form-item>
    <el-form-item label="上行内容" prop="replyContent">
      <el-input v-model="dataForm.replyContent" placeholder="上行内容" :disabled="true"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          batchNumber: '',
          mobilePhone: '',
          serialNumber: '',
          sendDate: '',
          requestTime: '',
          requestCount: '',
          requestStatus: '',
          messageId: '',
          replyContent: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          batchNumber: [
            { required: true, message: '批次号不能为空', trigger: 'blur' }
          ],
          mobilePhone: [
            { required: true, message: '用户手机号码不能为空', trigger: 'blur' }
          ],
          serialNumber: [
            { required: true, message: '序列号不能为空', trigger: 'blur' }
          ],
          sendDate: [
            { required: true, message: '发送日期不能为空', trigger: 'blur' }
          ],
          requestTime: [
            { required: true, message: '请求时间不能为空', trigger: 'blur' }
          ],
          messageId: [
            { required: true, message: '短信方返回的消息id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/customer/messagereply/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.batchNumber = data.messageReply.batchNumber
                this.dataForm.mobilePhone = data.messageReply.mobilePhone
                this.dataForm.serialNumber = data.messageReply.serialNumber
                this.dataForm.sendDate = data.messageReply.sendDate
                this.dataForm.requestTime = data.messageReply.requestTime
                this.dataForm.requestCount = data.messageReply.requestCount
                this.dataForm.requestStatus = data.messageReply.requestStatus
                this.dataForm.messageId = data.messageReply.messageId
                this.dataForm.replyContent = data.messageReply.replyContent
                this.dataForm.createUserId = data.messageReply.createUserId
                this.dataForm.createTime = data.messageReply.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/customer/messagereply/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'batchNumber': this.dataForm.batchNumber,
                'mobilePhone': this.dataForm.mobilePhone,
                'serialNumber': this.dataForm.serialNumber,
                'sendDate': this.dataForm.sendDate,
                'requestTime': this.dataForm.requestTime,
                'requestCount': this.dataForm.requestCount,
                'requestStatus': this.dataForm.requestStatus,
                'messageId': this.dataForm.messageId,
                'replyContent': this.dataForm.replyContent,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
